package cn.snmp.mutual.nms;

import java.math.*;

public class SnmpCounter64 extends SnmpVar
{
    long[] value;
    byte[] byteValue;
    
    public SnmpCounter64(final long l) {
        (this.value = new long[2])[0] = l;
        this.value[1] = ((l < 0L) ? 1 : 0);
        this.Type = 70;
    }
    
    public SnmpCounter64(final BigInteger biginteger) {
        this.value = new long[2];
        if (biginteger != null) {
            BigInteger biginteger2 = biginteger.shiftRight(32);
            this.value[1] = biginteger2.longValue();
            biginteger2 = biginteger.and(new BigInteger("FFFFFFFF", 16));
            this.value[0] = biginteger2.longValue();
        }
        this.Type = 70;
    }
    
    public SnmpCounter64(final long[] al) {
        this.value = new long[2];
        if (al != null) {
            this.value[0] = al[0];
            this.value[1] = al[1];
        }
        this.Type = 70;
    }
    
    public long[] counter64Diff(final SnmpCounter64 snmpcounter64) {
        final long[] al = new long[2];
        final BigInteger biginteger = new BigInteger("FFFFFFFFFFFFFFFF", 16);
        final BigInteger biginteger2 = new BigInteger("0000000000000000", 16);
        final BigInteger biginteger3 = new BigInteger("0");
        final BigInteger biginteger4 = new BigInteger("1");
        BigInteger biginteger5 = this.getValue(this);
        BigInteger biginteger6 = this.getValue(snmpcounter64);
        final int i = biginteger5.compareTo(biginteger3);
        final int j = biginteger6.compareTo(biginteger3);
        if (i >= 0 && j >= 0) {
            final BigInteger biginteger7 = biginteger5.subtract(biginteger6);
            if (biginteger7.compareTo(biginteger3) == -1) {
                biginteger5 = biginteger5.add(biginteger).subtract(biginteger6);
                biginteger5.add(biginteger4);
            }
            else {
                biginteger5 = biginteger7;
            }
        }
        else if (i < 0 && j < 0) {
            biginteger5 = biginteger6.subtract(biginteger5);
        }
        else if (i < 0) {
            final BigInteger biginteger8 = biginteger5.and(biginteger);
            if (biginteger6.compareTo(biginteger8) <= 0) {
                biginteger5 = biginteger2;
            }
            else {
                biginteger5 = biginteger.subtract(biginteger6).add(biginteger8).add(biginteger4);
            }
        }
        else if (j < 0) {
            final BigInteger biginteger9 = biginteger6.and(biginteger);
            if (biginteger5.compareTo(biginteger6.and(biginteger)) == 1) {
                biginteger5 = biginteger5.subtract(biginteger9).subtract(biginteger).subtract(biginteger4);
            }
        }
        else {
            biginteger5 = biginteger2;
        }
        biginteger6 = biginteger5.shiftRight(32);
        al[1] = biginteger6.longValue();
        biginteger6 = biginteger5.and(new BigInteger("FFFFFFFF", 16));
        al[0] = biginteger6.longValue();
        return al;
    }
    
    public long diff(final SnmpCounter64 snmpcounter64) {
        final long l = Long.MAX_VALUE;
        final long l2 = this.value[1] << 32 | this.value[0];
        final long l3 = snmpcounter64.value[1] << 32 | snmpcounter64.value[0];
        if (l2 >= 0L && l3 >= 0L) {
            final long l4 = l2 - l3;
            if (l4 < 0L) {
                return l - l3 + (l2 & l) + 1L;
            }
            return l4;
        }
        else {
            if (l2 < 0L && l3 < 0L) {
                return l3 - l2;
            }
            if (l2 < 0L) {
                if (l3 <= (l2 & l)) {
                    return Long.MIN_VALUE;
                }
                return l - l3 + (l2 & l) + 1L;
            }
            else {
                if (l3 < 0L && l2 > (l3 & l)) {
                    return l2 - (l3 & l) - l - 1L;
                }
                return Long.MIN_VALUE;
            }
        }
    }
    
    @Override
    int encode(final byte[] abyte0, final int i) throws ArrayIndexOutOfBoundsException {
        return ASNTypes.encodeUInt(abyte0, i, this.value, 70);
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (obj == null || !(obj instanceof SnmpCounter64)) {
            return false;
        }
        final SnmpCounter64 snmpcounter64 = (SnmpCounter64)obj;
        final long[] al = (long[])this.toValue();
        final long[] al2 = (long[])snmpcounter64.toValue();
        return al[0] == al2[0] && al[1] == al2[1];
    }
    
    private BigInteger getValue(final SnmpCounter64 snmpcounter64) {
        BigInteger biginteger = new BigInteger(new Long(snmpcounter64.value[1]).toString());
        biginteger = biginteger.shiftLeft(32);
        biginteger = biginteger.or(new BigInteger(new Long(snmpcounter64.value[0]).toString()));
        return biginteger;
    }
    
    @Override
    public Object getVarObject() {
        return this.toValue();
    }
    
    public BigInteger toBigInteger() {
        return this.getValue(this);
    }
    
    @Override
    public byte[] toBytes() {
        if (this.byteValue == null) {
            this.byteValue = this.getValue(this).toByteArray();
        }
        return this.byteValue;
    }
    
    @Override
    public String toString() {
        return "0x" + this.toBigInteger().toString(16);
    }
    
    @Override
    public String toTagString() {
        return "COUNTER64 : " + this.toString();
    }
    
    @Override
    public Object toValue() {
        final long[] al = { this.value[0], this.value[1] };
        return al;
    }
}
